import requests
import time

from bs4 import BeautifulSoup
from selenium import webdriver
# 下面这个构建隐式等待
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
# 可以对网页上元素是否存在，可点击等等进行判断，一般用于断言或与WebDriverWait配合使用。
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait


# --------------------果然是爬取了个寂寞--------------------------------------
# url = 'http://47.103.13.124:8001/many_sign'
# header = {
#     'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0'
# }
# cookies = {
# 	'session':'.eJyrViotTi1SsqpWyiyOT0zJzcxTsjLQUcrJTwexSopKU3WUcvOTMnNSlayUDM3gQEkHrDE-M0XJyhjCzkvMBSmKKTVNMjMDkiamFkq1tQDfeR3n.YMSFxw.zqmqHWReTQy-hZrWiGfXnUzvbZA'
# }

# b = requests.get(url,headers=header,cookies=cookies)
# t = BeautifulSoup(b.text,'lxml')
# print(t)
# --------------------果然是爬取了个寂寞--------------------------------------
url = 'http://47.103.13.124:8001/many_sign'
brower = webdriver.Chrome(executable_path='chromedriver.exe')
def wait_element(brower,element_id,wait_time=10):
    try:
        # 调用该方法提供的驱动程序作为参数，直到返回值为True
        WebDriverWait(brower,wait_element,1).until(
            # 判断是否至少有1个元素存在于dom树中。
            # 举个例子，如果页面上有n个元素的class都是'column-md-3'，那么只要有1个元素存在，这个方法就返回True
            EC.presence_of_all_elements_located((By.ID,element_id))
        )
    except Exception as e:
        raise Exception(e)

try:
    brower.get(url)
    # 如何用webdriver保存登录信息呢
    brower.add_cookie({'name':'session','value':'.eJyrViotTi1SsqpWyiyOT0zJzcxTsjLQUcrJTwexSopKU3WUcvOTMnNSlayUDM3gQEkHrDE-M0XJyhjCzkvMBSmKKTVNMjMDkiamFkq1tQDfeR3n.YMSFxw.zqmqHWReTQy-hZrWiGfXnUzvbZA'})
    # brower.session_id('.eJyrViotTi1SsqpWyiyOT0zJzcxTsjLQUcrJTwexSopKU3WUcvOTMnNSlayUDM3gQEkHrDE-M0XJyhjCzkvMBSmKKTVNMjMDkiamFkq1tQDfeR3n.YMSFxw.zqmqHWReTQy-hZrWiGfXnUzvbZA')
    # 为啥需要再次方位呢
    print('5')
    brower.get(url)
    time.sleep(5)
    apps = ['app1','app2','app3','app4','app5']
    result = []
    for i in apps:
        # 原来是不能链式进行写的
        # chu = brower.find_element_by_id(i).find_element_by_tag('img').get_attitude('src')
        chu = brower.find_element_by_id(i)
        src = chu.find_element_by_tag_name('img').get_attribute('src')
        title = chu.find_element_by_tag_name("h5").text
        jianjie = chu.find_element_by_tag_name('p').text
        scroe = chu.find_elements_by_tag_name('small')[0].text
        jieLun = chu.find_elements_by_tag_name('small')[1].text
        # print(src)
        result.append(
            {'src':src,
            'title':title,
            'jianjie':jianjie,
            'scroe':scroe,
            'jieLun':jieLun
        })

finally:
    print(result)
    time.sleep(10)
    brower.close()


















